Skip to content

perf(shader): give a 32-bit BDA store one write lookup - #5

Draft
Stan2032 wants to merge 1 commit into
mainfrom
topic/shader-bda-store-one-lookup
Draft

Stan2032 wants to merge 1 commit into
mainfrom
topic/shader-bda-store-one-lookup

Conversation

@Stan2032

@Stan2032 Stan2032 commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

What

  • A 32-bit BDA store through a bound V# (EmitBdaStore) or a computed one (EmitBdaWrite) emitted two copies of the write path at every store site. The caller emitted an aligned dword path (get_bda_write_pointer and note_bda_write). For an unaligned address it called write_bda_bytes, whose byte loop has its own lookup and note. Mesa and AMD's LLPC inline both even though the helpers are DontInline (3655, 25774), so every 32-bit store carried two page-table walks and two written-page probes.
  • write_bda_bytes now also takes the aligned dword. Given 4 bytes at a 4-aligned address, its loop runs once with a 4-byte lookup and a dword store. Otherwise it runs once per byte, as before. Both store paths now call it once.
  • The dword-by-dword fallback of a multi-dword write (25774) goes through the same call, so it drops from 2 write lookups to 1.
  • Each address is looked up, stored, faulted and noted as before:
    • An aligned dword that is not mapped as one writable piece is still one fault, with no bytes stored.
    • An unaligned dword still goes byte by byte.
    • EmitBdaWrite still checks overflow only for 8- and 16-bit writes and for unaligned dwords.
  • BdaContracts: every 8-, 16- and 32-bit write through a bound or a runtime V# now has 1 write lookup and 1 byte-loop call, and the multi-dword fallback has 1 write lookup. With main's SpirvBdaRead.cpp it fails with a bound V# write of 32 bits has 2 write lookups and 1 byte-loop calls, not one write lookup in write_bda_bytes.

Context: 13538. There, the lookup chain is about 60% of the instructions of the modules RADV cannot compile. This PR removes one write lookup and one note per 32-bit store. It does not touch reads, DefineBdaLookup or note_bda_write. On the title itself the change does little: dantdmdarealguy compiled the real 0x1ac4 and 0x1b14 programs with it on an RTX 5080 (driver 621.14), and the difference was about 5%, inside the 5-8% run-to-run noise, with about 2% less peak memory. Those modules have only 6 BDA store sites; their size is in the reads (2,101 read_bda_span_stop and 4,166 record_bda_fault calls) and the run-time image mode switches. So the numbers below apply to store-heavy programs, not to that one. 22944 removes the note_bda_write calls next to the stores this PR moves, so whichever of the two lands second drops or keeps that one call; the single lookup per store stays either way.

Size. I measured a synthetic table-call program: the waterfall s_swappc_b64 of the SplitTableCallRun tests, linked to N callees. Each callee does 8 global_load_dword and 4 or 8 global_store_dword through a pointer from user data.

  • Each module was recompiled, then fully inlined: DontInline was cleared, which approximates what the driver compiles.
  • It was optimized with spirv-opt -O with StorePages (spec constant 8239) frozen to 0 or 1, and its instructions were counted.
callees, stores per callee main this PR
4, 8 stores, StorePages=0 57,503 45,055 (−22%)
4, 8 stores, StorePages=1 73,633 56,417 (−23%)
4, 4 stores, StorePages=0 43,527 37,302 (−14%)
4, 4 stores, StorePages=1 54,889 46,280 (−16%)

That is about 390 fewer instructions per 32-bit store with StorePages=0 and about 540 with StorePages=1. A store cost about 930 and 1,430 instructions before. Emitted SPIR-V shrinks by about 75 words per store.

For a driver that keeps the calls, each 32-bit store site in main now makes 1 call instead of 3. In the 4-callee, 8-store program that is 192 calls from main down to 128. 6706 found that NVIDIA's pipeline creation time tracks the number of calls.

Compile time on lavapipe. These are the same programs with MESA_SHADER_CACHE_DISABLE=true. lavapipe compiles LLVM at the first dispatch. The runs alternate main, PR, PR, main with the same test binary; only libSceAgcDriver.prx differs.

4 callees main: first dispatch, peak RSS this PR: first dispatch, peak RSS
8 loads + 8 stores per callee 229.1 s, 212.6 s; 3.19 GiB 78.5 s, 72.7 s; 1.42 GiB
8 loads + 4 stores per callee 75.2 s, 69.0 s; 1.38 GiB 38.0 s, 38.6 s; 0.78 GiB

Pipeline creation itself (NIR) took 4.5 and 4.0 s on main against 3.1 and 1.7 s with this PR (8 stores per callee), and 2.7 and 1.8 s against 1.3 and 1.1 s (4 stores). Every run wrote all 32 lanes.

Pipeline creation on RADV. I compiled the same SPIR-V modules with RADV (Mesa 25.2.8, ACO) through Mesa's amdgpu_noop_drm_shim, which lets RADV build pipelines without the GPU. A small standalone program builds one compute pipeline with the module's layout: storage buffers at bindings 0 (3 of them), 59, 60 and 61, 128 bytes of push constants, and a required subgroup size of 32. It times vkCreateComputePipelines and reads the process's peak RSS, with MESA_SHADER_CACHE_DISABLE=true. The validation layer reports nothing for this setup. Peak RSS is the same on every run. Time varies by up to about 25% between runs on this shared machine, so the 12- and 16-callee rows on RDNA2 list three runs each.

callees, stores per callee RDNA2 (AMDGPU_GPU_ID=navi21), main this PR RDNA4 (gfx1201), main this PR
4, 8 4.4 s, 511 MiB 3.0 s, 341 MiB 5.8 s, 506 MiB 3.4 s, 341 MiB
8, 8 12.8 s, 1,293 MiB 6.8 s, 862 MiB 11.9 s, 1,292 MiB 8.7 s, 845 MiB
12, 4 21.2 / 16.1 / 18.8 s, 1,803 MiB 10.4 / 9.1 / 9.5 s, 1,251 MiB 16.0 s, 1,830 MiB 10.0 s, 1,249 MiB
12, 8 41.3 / 29.6 / 37.9 s, 2,912 MiB 21.2 / 17.3 / 25.9 s, 1,841 MiB 31.8 s, 2,942 MiB 17.5 s, 1,781 MiB
16, 4 36.1 / 29.3 / 31.1 s, 3,346 MiB 17.6 / 18.8 / 18.7 s, 2,362 MiB 34.6 s, 3,429 MiB 20.5 s, 2,309 MiB
16, 8 60.6 / 58.0 / 81.8 s, 5,445 MiB 31.0 / 31.6 / 40.9 s, 3,391 MiB 64.8 s, 5,443 MiB 34.3 s, 3,307 MiB

On RADV, pipeline creation takes about half as long, and peak memory drops by 30-40%. Memory grows faster than linearly with the number of callees on both sides, as in 13538.

Together with 22944. 22944 (head ac8141c, merged with main 76d0a4a) removes note_bda_write. I also applied this PR on top of it, keeping the single write lookup per store and dropping its one note call. The table shows RADV through the shim on RDNA2. Each pairing gives the same output and memory hashes, and agc_driver_bda_device passes on the combination. Both PRs rewrite the 32-bit store path in SpirvBdaRead.cpp (EmitBdaStoreAt and the store body), so they conflict textually; the numbers below come from a hand merge, and whichever merges second needs a rebase.

callees, stores per callee main this PR 22944 22944 + this PR
4, 8 513 MiB 341 MiB 277 MiB, 2.2 s 235 MiB, 2.2 s
8, 8 1,295 MiB 862 MiB 672 MiB, 6.9 s 533 MiB, 5.1 s
16, 4 3,348 MiB 2,362 MiB 1,944 MiB, 26.2 s 1,629 MiB, 16.0 s
16, 8 5,446 MiB 3,391 MiB 2,533 MiB, 40-42 s 2,057 MiB, 28-31 s

The two changes add up: on top of 22944, this PR still cuts the driver-inlined size by 20% (224,628 to 179,598 instructions for 16 callees with 8 stores each).

For 1, 2 and 4 callees with 4 or 8 stores each, I hashed the program's output and the guest block it stores into after the dispatch, with address randomization off so the block's buffer descriptor matches. All 6 programs hash the same on main and on this PR.

Pipeline creation on NVIDIA. These are the same modules on a GeForce GTX 1050, Windows 11, driver 582.67, Vulkan 1.4.312. The program is the same standalone pipeline tool, built for Windows; it adds a random OpSourceExtension to each module so the driver's shader cache cannot serve it. Each cell is the median of 4 runs over two rounds in alternating order, giving vkCreateComputePipelines time and peak process memory.

callees, stores per callee main this PR 22944 22944 + this PR
4, 8 581 ms, 91 MiB 216 ms (2.7x faster), 71 MiB 257 ms, 72 MiB 214 ms, 71 MiB
8, 8 1,584 ms, 132 MiB 388 ms (4.1x), 81 MiB 507 ms, 86 MiB 396 ms, 80 MiB
16, 8 5,932 ms, 234 MiB 895 ms (6.6x), 103 MiB 1,389 ms, 114 MiB 910 ms, 101 MiB

On NVIDIA, main's time grows 2.7x and then 3.7x for each doubling of callees, and this PR's 1.8x and then 2.3x, so the gap widens with size. On this driver, 22944 adds nothing on top of this PR.

The tools and scripts behind these numbers, with the drm-shim setup, are on bench/compute-shader-cost in my fork.

Tested

Ubuntu 24.04.5, kernel 6.18, lavapipe (llvmpipe, Mesa 25.2.8, LLVM 20.1.2), and RADV (Mesa 25.2.8) compiling only, through the drm-shim. No hardware GPU on this machine.

  • agc_driver_bda_device with ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1 and the Khronos validation layer 1.4.363.1 passes, with no VUID- line.
    • On a CPU device it runs the store boundary cases: byte, split short, split unaligned dword, read-only byte, short suffix and suffix, unmapped middle byte, and split aligned dword. Each case runs through a bound and a runtime V#, plain and coherent.
    • It skips the multi-dword write cases on a CPU device. With that skip removed locally (not in this PR), they pass on main and on this PR. They run unmodified on NVIDIA; see below.
  • I ran the 301 ctest tests that match bda|store|_write|flat|global|runtime_format|runtime_buffer|shader_memory|atomic|buffer_load|unaligned|straddle|masked (host-only suites excluded) with the validation layer. Both runs used the same test binaries; only libSceAgcDriver.prx differed.
    • This PR: 298 pass.
    • main: 297 pass.
    • Three fail or don't run the same way on both, with the same messages: agc_driver_flat_aperture_lds_bounds (28316 fixes it), agc_driver_write_back_guest_newer, and agc_driver_image_store_volume_through_plane (not built).
    • main also fails agc_driver_bda_device, on the new contract check.
    • No VUID- line in either run.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check pass.

On NVIDIA hardware, Dec 1: NVIDIA GeForce GTX 1050 (the machine's only Vulkan device), driver 582.67, Vulkan 1.4.312, validation layer 1.4.363, Windows 11, built with WinLibs GCC 15.2.0. This branch is 30 commits behind main, so I applied its commit onto main 483b81f and compared that with main 483b81f on the same machine; the branch head itself was not built.

  • The 273 tests matching bda|store|_write, with ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer: 270 pass, 0 fail and 3 skip on both main and this change. The skipped tests are agc_driver_host_write_shadowed_unit, agc_driver_idle_store_over_unmap and agc_driver_mesh_vector_window_writes, on both.
  • agc_driver_bda_device, which holds the new contract check, passes with this change. On this GPU it also runs the BDA read and multi-dword write execution cases that it skips on a CPU device; they pass on both main and this change.
  • The loader inserted the layer in 147 test logs on each side, and no log has a VUID- line.

Not tested: AMD hardware, AMD's own driver (LLPC), and any title. The RADV numbers come from the shim and the NVIDIA timings from pipeline creation only, so both cover compilation; only the NVIDIA ctest run above executed on a GPU. Numbers from a real store-heavy compute program would be the useful next measurement.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: lavapipe (llvmpipe, Mesa 25.2.8, LLVM 20.1.2), Ubuntu 24.04.5; RADV (Mesa 25.2.8) pipeline creation only, as RDNA2 (navi21) and RDNA4 (gfx1201) through Mesa's amdgpu drm-shim; NVIDIA GeForce GTX 1050 (driver 582.67, Windows 11): pipeline creation, and the BDA and store tests with this commit applied onto a newer main
  • AI-assisted: yes

https://claude.ai/code/session_0125gxvaRgLMwjs53eqvUzya


Generated by Claude Code

A 32-bit store through a bound or a computed V# emitted two
get_bda_write_pointer calls at every store site, one for an aligned
dword in the caller and one in write_bda_bytes for the unaligned byte
path, each followed by its own note_bda_write. Drivers inline both, so
every store carried two copies of the page-table walk and of the
written-page probe.

write_bda_bytes now also takes the aligned dword: its loop runs once
with a 4-byte lookup and a dword store, and otherwise once per byte as
before. EmitBdaStore and EmitBdaWrite call it once, and the
dword-by-dword fallback of a multi-dword write gets the same single
lookup. Each address is looked up, stored, faulted and noted as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0125gxvaRgLMwjs53eqvUzya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants